package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func getIntersectionNode(headA, headB *ListNode) *ListNode {
	//temp1:=headA
	//temp2:=headB
	//for  {
	//	if temp1==nil||temp2==nil {
	//		break
	//	}
	//	if temp1==temp2{
	//		return temp1
	//	}
	//	temp1 = temp1.Next
	//	temp2 = temp2.Next
	//}
	//return nil
	vis := map[*ListNode]bool{}
	for tmp := headA; tmp != nil; tmp = tmp.Next {
		vis[tmp] = true
	}
	for tmp := headB; tmp != nil; tmp = tmp.Next {
		if vis[tmp] {
			return tmp
		}
	}
	return nil
}
func main() {
	head1 := &ListNode{1, nil}
	node1 := &ListNode{2, nil}
	head1.Next = node1
	node2 := &ListNode{3, nil}
	node1.Next = node2
	node3 := &ListNode{4, nil}
	node2.Next = node3
	node4 := &ListNode{5, nil}
	node3.Next = node4
	head2 := &ListNode{1, nil}
	node5 := &ListNode{3, nil}
	head2.Next = node5
	node6 := &ListNode{6, nil}
	node5.Next = node6
	node6.Next = node3
	res := getIntersectionNode(head1, head2)
	fmt.Println(res.Val)

}
